package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.OrderClientService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.CommonUtils;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.user.bean.Morder;
import cn.hinglo.gameclaw.user.bean.Orders;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @createTime: 2018/1/4
 * @author: HingLo
 * @description: 订单管理App端的管理
 */
@RestController
@RequestMapping("/client/oc/")
public class OrderClientAction extends BaseController {
    private final OrderClientService orderClientService;

    @Autowired
    public OrderClientAction(OrderClientService orderClientService) {
        this.orderClientService = orderClientService;
    }

    /**
     * 根据用户的ID查询用户的发货的订单,只能查看，代发货，完成，待收货的三种状态的订单信息
     *
     * @return 操作结果 ，订单列表
     */
    @GetMapping("list")
    public Result listOrder() {
        String userId = getUserId(request);
        List<Orders> list = orderClientService.listOrderService(userId);
        List<Map<String, Object>> res = new ArrayList<>(list.size());
        list.forEach(x -> {
            int status = x.getStatus();
            if (status == FieldUtils.ORDER_0 || status == FieldUtils.ORDER_1 || status == FieldUtils.ORDER_2) {
                Map<String, Object> r = new HashMap<>();
                r.put("id", x.getId());
                r.put("name", x.getBody());
                r.put("date", x.getDate());
                //地址Id
                r.put("aid", x.getAid());
                //物流Id
                r.put("gid", x.getGid());
                //头像地址
                r.put("header", CommonUtils.imgInstall(x.getHeader(), FieldUtils.IMAGE_3));

                if (status == FieldUtils.ORDER_0) {
                    r.put("status", "待发货");
                } else if (status == FieldUtils.ORDER_1) {
                    r.put("status", "待收货");
                } else {
                    r.put("status", "完成");
                }
                res.add(r);
            }

        });
        return success(res);
    }


    /**
     * 充值记录
     *
     * @return 操作结果，充值记录列表
     */
    @GetMapping("recharge")
    public Result listRecharge() {
        String userId = getUserId(request);
        List<Morder> list = orderClientService.listRechargeService(userId);

        //处理成客户端需要的数据
        List<Map<String, Object>> mapList = new ArrayList<>(list.size());
        list.forEach(action -> {
            Map<String, Object> map = new HashMap<>();
            map.put("num", action.getNum());
            map.put("gold", action.getGold());
            map.put("giveGold", action.getGiveGold());
            map.put("payDate", DateUtil.format(action.getPayDate(), "yyyy-MM-dd HH:mm:ss"));
            map.put("payment", action.getPayment() == FieldUtils.PAYMENT_1 ? "支付宝" : "微信");
            mapList.add(map);
        });


        return success(mapList);
    }

    /**
     * 物流信息查看
     *
     * @param id 物流信息的唯一Id
     * @return 操作结果
     */
    @GetMapping("good")
    public Result findGoods(String id) {
        return orderClientService.findByIdGoodService(id);
    }


}
